package com.dqp.cslggroup.Library;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.dqp.cslggroup.R;
import com.dqp.cslggroup.UI.BaseActivity;

import java.util.Objects;

public class Lib_web extends BaseActivity {
    private String url;
    private WebView mWebView;
    private ProgressBar pb;
    private LinearLayout mErrorView;
    private boolean loadError = false;

    @SuppressLint("SetJavaScriptEnabled")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web);
        Toolbar toolbar = findViewById(R.id.include_toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
        Intent intent = getIntent();
        url = intent.getStringExtra("url");
        mWebView = findViewById(R.id.web);
        mErrorView = findViewById(R.id.web_error);
        mWebView.clearCache(true);
        mWebView.getSettings().setJavaScriptEnabled(true);
        pb = findViewById(R.id.w_pb);
        TextView imageView = findViewById(R.id.seting);
        imageView.setVisibility(View.VISIBLE);
        mWebView.getSettings().setJavaScriptEnabled(true);       //支持javascript
        mWebView.getSettings().setSupportZoom(true);        // 设置可以支持缩放
        mWebView.getSettings().setBuiltInZoomControls(true);        // 设置出现缩放工具
        mWebView.getSettings().setUseWideViewPort(true);        //扩大比例的缩放
        mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);       //自适应屏幕
        mWebView.getSettings().setLoadWithOverviewMode(true);
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    pb.setVisibility(View.GONE);//加载完网页进度条消失
                } else {
                    pb.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    pb.setProgress(newProgress);//设置进度值
                }
            }
        });
        mWebView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl) {

                super.onReceivedError(view, errorCode, description, failingUrl);
                view.setVisibility(View.GONE);
                mErrorView.setVisibility(View.VISIBLE);
                loadError = true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (!loadError) {
                    mErrorView.setVisibility(View.GONE);
                    mWebView.setVisibility(View.VISIBLE);
                }
            }

        });
        imageView.setOnClickListener(view -> {

            Intent intent1 = new Intent(Lib_web.this, Login_library.class);
            startActivity(intent1);
        });

        mWebView.loadUrl(url);

    }

    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            if (mWebView.getUrl().equals(url)) {
                super.onBackPressed();
            } else {
                mWebView.goBack();
            }
        } else {
            super.onBackPressed();
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}


